package behavior.cor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lzp
 * @Date:2023/2/27
 * @description: 处理器链
 */
public class HandlerChain {

	public List<Handler> handlers = new ArrayList<Handler>();

	public void addHandler(Handler handler) {
		this.handlers.add(handler);
	}

	public boolean process(Request request) {
		// 依次调用每个handler
		for (Handler handler : handlers) {
			Boolean result = handler.process(request);
			if (result != null) {
				// 如果返回TRUE或FALSE，处理结束:
				System.out.println(request + " " + (result ? "请求已被核准，审核人=>" : "请求被否认，审核人=> ") + handler.getClass().getSimpleName());
				return result;
			}
		}
		throw new RuntimeException("无法处理请求: " + request);
	}

}
